<!--  -->
<template>
  <div class="cart">
    <div class="header-cart">购物车</div>
    <div class="nocart" v-if="cartDatas.length==0">
      <i class="iconfont icon-gouwuchekong"></i>
      <p>
        购物车空空如也!
        <router-link to="/home">去逛逛</router-link>
      </p>
    </div>
    <div class="cart-list" v-else>
      <ul>
        <li v-for="(cartData,index) in cartDatas" :key="index">
          <div class="select" @click="changeSelect(cartData,index)">
            <i class="iconfont icon-danxuankuang" v-if="!cartData.isSelect"></i>
            <i class="iconfont icon-danxuankuangxuanzhong" style="color:#f55" v-else></i>
          </div>
          <div class="cart-img">
            <img :src="cartData.goods_thumb" alt />
          </div>
          <div class="cart-content">
            <div class="cart-title">{{cartData.goods_name}}</div>
            <div class="wrap">
              <div class="price">{{cartData.shop_price}}</div>
              <div class="num-wrap">
                <a href="javascript:;" @click="jianFn(index)">-</a>
                <input type="text" v-model="cartData.value" />
                <a href="javascript:;" @click="addFn(index)">+</a>
                <i class="iconfont icon-shoucang1"></i>
                <i class="iconfont icon-shanchu1" @click="deleteFn(index)"></i>
              </div>
            </div>
          </div>
        </li>
      </ul>
    </div>

    <div class="cart-footer" v-if="cartDatas.length>0">
      <div class="selectAll" @click="selectAllFn">
        <i class="iconfont icon-danxuankuang" v-if="!selectAll"></i>
        <i class="iconfont icon-danxuankuangxuanzhong" v-else style="color:#f55"></i>
      </div>
      <div class="totalPrice">
        <div class="price">
          <span>合计</span>
          <span>{{totalPrice}}</span>
        </div>
        <p>（不含运费，以节省0元）</p>
      </div>
      <div class="tobuy">去结算</div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {};
  },
  computed: {
    cartDatas() {
      return this.$store.state.cartDatas;
    },
    selectAll() {
      return this.$store.state.selectAll;
    },
    totalPrice() {
      let priceAll = 0;
      this.$store.state.cartDatas.map((item) => {
        if (item.isSelect) {
          priceAll += item.value * item.shop_price;
        }
      });
      return priceAll;
    },
  },
  methods: {
    changeSelect(cart, index) {
      console.log(cart);
      console.log(index);
      cart.isSelect = !cart.isSelect;
      this.$store.commit("changeSelect", { cart: cart, index: index });
    },
    selectAllFn() {
      this.$store.commit("selectAllFn");
    },
    addFn(index) {
      this.$store.commit("addFn", index);
    },
    jianFn(index) {
      this.$store.commit("jianFn", index);
    },
    deleteFn(index) {
      this.$store.commit("deleteFn", index);
    },
  },
};
</script>
<style lang="less">
.cart {
  .nocart {
    text-align: center;
    height: 100vh;
    i {
      font-size: 20rem;
      color: #999;
    }
  }
  .header-cart {
    width: 100%;
    height: 4.4rem;
    text-align: center;
    font-size: 1.8rem;
    line-height: 4.4rem;
    background: #ccc;
  }
  .cart-list {
    width: 100%;
    height: auto;

    li {
      display: flex;
      width: 100%;
      justify-content: space-between;
      align-items: center;
      height: 10.4rem;
      background: #fff;
      border-bottom: 1px solid #ccc;
      .select {
        width: 10%;
        text-align: center;
      }
      .cart-img {
        img {
          width: 7rem;
          height: 7rem;
          margin: 0 1rem;
        }
      }
      .cart-content {
        margin-right: 1rem;
        .cart-title {
          height: 4rem;
          font-size: 1.4rem;
          line-height: 2rem;
          overflow: hidden;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
        }
        .wrap {
          height: 4.4rem;
          display: flex;
          justify-content: space-between;
          .price {
            font-size: 2rem;
            line-height: 4.4rem;
            color: #f55;
            &::before {
              content: "￥";
            }
          }
          .num-wrap {
            display: flex;
            height: 4.4rem;
            align-items: center;
            a {
              width: 3rem;
              height: 3rem;
              text-align: center;
              line-height: 3rem;
              display: block;
              font-size: 2rem;
              background: #efefef;
            }
            input {
              width: 5rem;
              height: 3rem;
              border: 1px solid #ccc;
              box-sizing: border-box;
              text-align: center;
            }
            i {
              margin: 0 1rem;
            }
          }
        }
      }
    }
  }
  .cart-footer {
    width: 100%;
    height: 4.4rem;
    position: fixed;
    bottom: 5rem;
    left: 0;
    background-color: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .selectAll {
      width: 10%;
      text-align: center;
    }
    .totalPrice {
      width: 60%;
      margin-right: 1rem;
      text-align: right;
      span {
        &:last-child {
          font-size: 2rem;
          color: #f55;
          &::before {
            content: "￥";
            font-size: 1.4rem;
          }
        }
      }
    }
    .tobuy {
      width: 30%;
      height: 4.4rem;
      background: #f55;
      color: #fff;
      line-height: 4.4rem;
      text-align: center;
      font-size: 1.6rem;
    }
  }
}
</style>